<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/commons/global.jsp"%>

<div id="notEntityCategoryChildrenLayoutConfig-div"
	class="easyui-window" title="分类集合配置" style="width: 96%; height: 96%;"
	data-options="iconCls:'icon-cog',modal:true,openAnimation:'fade',closeAnimation:'slide',cache:false,collapsible:false,minimizable:false,maximizable:false,resizable:false">
	<div id="notEntityCategoryChildrenLayoutConfig"
		style="width: 100%; height: 100%;">
		<div data-options="region:'east',split:true,collapsible:true"
			style="height: 30%;" title="子分类列表">
			<div style="height: auto; width: 100%; padding: 10px;">
				<a type="button" class="btn btn-default"
					onclick="relationUpdate(false)"><i class="fa fa-minus">删除</i></a> <a
					type="button" class="btn btn-default"
					onclick="notEntityCategoryLayoutTogget(false)"><i
					class="fa fa-ellipsis-v">添加</i></a>
			</div>
			<div class="cur-not-entity-Category-children-wrapper"
				style="width: 100%; height: 100%;">
				<table id="curNotEntityCategoryChildren"
					style="width: 100%; height: 100%;"></table>
			</div>
		</div>
		<div
			data-options="region:'center',split:true,collapsible:true,hideCollapsedContent:false"
			style="height: 70%;" title="分类列表">
			<div style="height: auto; width: 100%; padding: 10px;">
				<a type="button" class="btn btn-default"
					onclick="relationUpdate(true)"><i class="fa fa-plus">添加</i></a> <a
					type="button" class="btn btn-default"
					onclick="notEntityCategoryLayoutTogget(true)"><i
					class="fa fa-leaf">子分类列表</i></a>
			</div>
			<div class="not-entity-Category-children-wrapper"
				style="width: 100%; height: 100%;">
				<table id="notEntityCategoryChildren"
					style="width: 100%; height: 100%;"></table>
			</div>
		</div>

	</div>
</div>

<script type="text/javascript">
	var notEntityCategoryChildrenDatagrid = undefined;
	$(function() {
		notEntityCategoryChildrenDatagrid = $('#notEntityCategoryChildren')
				.datagrid(
						{
							url : '${baseUrl}/category/list',
							pagination : true,
							sortName : 'sort,updateTime,ckey',
							sortOrder : 'desc,desc,asc',
							multiSort : true,
							//remoteSort : false,
							// 			rownumbers : true,

							queryParams : {
								entity : true,
								parentId : '${param.id}'
							},
							singleSelect : true,
							idField : 'id',
							striped : true,
							pageSize : 20,
							fitColumns : true,
							toolbar : '#categoryListToolBars',
							onLoadSuccess : function() {
								categorySelectIndex = undefined;
								categorySelectRow = undefined;
								categoryEditIndex = undefined;
								categoryEditRow = undefined;
							},
							onBeforeSelect : function(index, row) {
								if (categoryEditIndex != undefined) {
									return false;
								} else {
									categorySelectIndex = index;
									categorySelectRow = row;
								}
							},
							onBeforeEdit : function(index, row) {
								if (categoryEditIndex == undefined) {
									categoryEditIndex = index;
									categoryEditRow = row;
								}
							},
							onEndEdit : function(index, row, changes) {
							},
							// 			nowrap : false,
							columns : [ [
									{
										field : 'id',
										title : 'ID',
										align : 'center',
										width : 60
									},
									{
										field : 'text',
										title : '名称',
										width : 249,
										editor : {
											type : 'text',
											options : {
												required : true
											}
										}
									},
									{
										field : 'ckey',
										title : '类型KEY',
										align : 'center',
										width : 100,
										sortable : true
									},
									{
										field : 'itemTypeMsg',
										title : '资源类型',
										align : 'center',
										width : 100
									},
									{
										field : 'createTime',
										title : '创建时间',
										width : 100,
										align : 'left'
									},
									{
										field : 'updateTime',
										title : '更新时间',
										width : 100,
										align : 'left',
										sortable : true
									},
									{
										field : 'createUser',
										title : '创建者',
										width : 60,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.createUser) {
												return row.createUser.nickname;
											} else {
												return value;
											}
										}
									},
									{
										field : 'entity',
										title : '分类实体',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											return value;
										}
									},
									{
										field : 'parent',
										title : '所属分类集合',
										width : 249,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.parent) {
												return "<span>集合ID:"
														+ row.parent.id
														+ "</span><br/><span>集合标题:"
														+ row.parent.text
														+ "</span>";
											} else {
												return value;
											}
										}
									}, {
										field : 'sort',
										title : '排序',
										width : 60,
										align : 'center',
										sortable : true,
										editor : {
											type : 'numberbox',
											options : {
												min : 0,
												precision : 2,
												required : true
											}
										}
									} ] ],
							onDblClickRow : function(index, row) {
								$.ajax({
									data : {
										id : row.id
									},
									async : false,
									url : baseUrl + '/category/info.html',
									success : function(html) {
										$('body').append(html);
									}
								});

							}
						});

		notEntityCategoryChildrenDatagrid.datagrid('getPager').pagination(
				{
					buttons : [ {
						iconCls : 'fa fa-2x fa-location-arrow',
						handler : function() {
							notEntityCategoryChildrenDatagrid.datagrid(
									'gotoPage',
									notEntityCategoryChildrenDatagrid.datagrid(
											'getPager').find('.pagination-num')
											.val());
						}
					} ]
				});
	});
</script>


<script type="text/javascript">
	var curNotEntityCategoryChildrenDatagrid = undefined;
	var curNotEntityCategoryChildEditRowIndex = undefined;
	var notEntityCategoryChildrenLayoutConfig = undefined;
	$(function() {
		var notEntityCategoryChildrenLayoutConfig_div = $(
				'#notEntityCategoryChildrenLayoutConfig-div').window({
			onBeforeClose : function() {

			},
			onClose : function() {
				notEntityCategoryChildrenLayoutConfig_div.panel('destroy');
				try {
					CategoryNotEntityListDatagrid.datagrid('reload');
				} catch (e) {

				}
			}
		});

		notEntityCategoryChildrenLayoutConfig = $(
				'#notEntityCategoryChildrenLayoutConfig').layout({
			onCollapse : function(region) {
				notEntityCategoryChildrenDatagrid.datagrid('reload');
			},
			onExpand : function(region) {
				curNotEntityCategoryChildrenDatagrid.datagrid('reload');
			}
		});

		curNotEntityCategoryChildrenDatagrid = $(
				'#curNotEntityCategoryChildren')
				.datagrid(
						{
							url : baseUrl + '/category/list',
							pagination : true,
							sortName : 'sort,updateTime',
							sortOrder : 'desc,desc',
							multiSort : true,
							//remoteSort : false,
							// 			rownumbers : true,
							queryParams : {
								isEntity : true,
								parentId : '${param.id}'
							},
							singleSelect : true,
							idField : 'id',
							striped : true,
							pageSize : 20,
							fitColumns : true,
							onBeforeSelect : function(index, row) {
								if (curNotEntityCategoryChildEditRowIndex != undefined) {
									return false;
								}
							},
							onLoadSuccess : function() {
								curNotEntityCategoryChildEditRowIndex = undefined;
							},
							onEndEdit : function(index, row, changes) {
								curNotEntityCategoryChildEditRowIndex = undefined;
							},
							onClickCell : function(index, field, value) {
								if (curNotEntityCategoryChildEditRowIndex != undefined) {
									if (!curNotEntityCategoryChildrenDatagrid
											.datagrid('validateRow',
													curNotEntityCategoryChildEditRowIndex)) {
										return;
									}

									$.messager
											.confirm(
													"确认",
													"确定修改排序么?",
													function(r) {
														if (r) {

															var CategorySelectRow = curNotEntityCategoryChildrenDatagrid
																	.datagrid('getSelected');

															//获取排序
															var _edSort = curNotEntityCategoryChildrenDatagrid
																	.datagrid(
																			'getEditor',
																			{
																				index : curNotEntityCategoryChildEditRowIndex,
																				field : 'sort'
																			});

															var _Sort = $(
																	_edSort.target)
																	.val();

															var _Data = {
																id : CategorySelectRow.id,
																title : CategorySelectRow.title,
																sort : _Sort,
																playtime : CategorySelectRow.playtime,
																summary : CategorySelectRow.summary,
																info : CategorySelectRow.info,
																status : CategorySelectRow.statusCode
															};

															$
																	.ajax({
																		data : _Data,
																		async : false,
																		type : 'POST',
																		url : baseUrl
																				+ '/Category/update',
																		success : function(
																				data) {
																			if (data.responseCode == '000') {
																				curNotEntityCategoryChildrenDatagrid
																						.datagrid(
																								'endEdit',
																								curNotEntityCategoryChildEditRowIndex);

																				curNotEntityCategoryChildrenDatagrid
																						.datagrid('acceptChanges');

																				$.messager
																						.alert(
																								'提示',
																								'修改成功',
																								'info');
																			} else {
																				$.messager
																						.alert(
																								'提示',
																								data.responseMessage,
																								'error');
																			}
																		}
																	});

														} else {
															curNotEntityCategoryChildrenDatagrid
																	.datagrid('rejectChanges');
															curNotEntityCategoryChildEditRowIndex = undefined;
														}
													});
									return;
								}

								if (curNotEntityCategoryChildEditRowIndex == undefined
										&& field == 'sort') {
									curNotEntityCategoryChildrenDatagrid
											.datagrid('selectRow', index)
											.datagrid('editCell', {
												index : index,
												field : field
											});
									curNotEntityCategoryChildEditRowIndex = index;
								}
							},
							columns : [ [
									{
										field : 'id',
										title : 'ID',
										align : 'center',
										width : 60
									},
									{
										field : 'text',
										title : '名称',
										width : 249,
										editor : {
											type : 'text',
											options : {
												required : true
											}
										}
									},
									{
										field : 'ckey',
										title : '类型KEY',
										align : 'center',
										width : 100,
										sortable : true
									},
									{
										field : 'itemTypeMsg',
										title : '资源类型',
										align : 'center',
										width : 100
									},
									{
										field : 'createTime',
										title : '创建时间',
										width : 100,
										align : 'left'
									},
									{
										field : 'updateTime',
										title : '更新时间',
										width : 100,
										align : 'left',
										sortable : true
									},
									{
										field : 'createUser',
										title : '创建者',
										width : 60,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.createUser) {
												return row.createUser.nickname;
											} else {
												return value;
											}
										}
									},
									{
										field : 'entity',
										title : '分类实体',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											return value;
										}
									},
									{
										field : 'parent',
										title : '所属分类集合',
										width : 249,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.parent) {
												return "<span>集合ID:"
														+ row.parent.id
														+ "</span><br/><span>集合标题:"
														+ row.parent.text
														+ "</span>";
											} else {
												return value;
											}
										}
									}, {
										field : 'sort',
										title : '排序',
										width : 60,
										align : 'center',
										sortable : true,
										editor : {
											type : 'numberbox',
											options : {
												min : 0,
												precision : 2,
												required : true
											}
										}
									} ] ]
						});

		curNotEntityCategoryChildrenDatagrid.datagrid('getPager').pagination(
				{
					buttons : [ {
						iconCls : 'fa fa-2x fa-location-arrow',
						handler : function() {
							curNotEntityCategoryChildrenDatagrid.datagrid(
									'gotoPage',
									curNotEntityCategoryChildrenDatagrid
											.datagrid('getPager').find(
													'.pagination-num').val());
						}
					} ]
				});

	});
	
	function notEntityCategoryList_remove

	function relationUpdate(optype) {
		var selectCurChildCategory = curNotEntityCategoryChildrenDatagrid
				.datagrid('getSelected');
		if (optype) {
			selectCurChildCategory = notEntityCategoryChildrenDatagrid
					.datagrid('getSelected');
		}
		if (selectCurChildCategory) {
			var showMsg = optype ? '添加' : '删除';

			$.messager
					.confirm(
							'确认',
							'您确认想要' + showMsg + '此配置关系吗？',
							function(r) {
								if (r) {
									var _Data = {
										childId : selectCurChildCategory.id,
										parentId : '${param.id}',
										optype : optype
									};

									$
											.ajax({
												data : _Data,
												async : false,
												type : 'POST',
												url : baseUrl
														+ '/Category/relation-update',
												success : function(data) {
													if (data.responseCode == '000') {
														$.messager.alert('提示',
																showMsg + '成功',
																'info');
														if (optype) {

														} else {
															$.messager.alert(
																	'提示',
																	'删除成功',
																	'info');
															curNotEntityCategoryChildrenDatagrid
																	.datagrid(
																			'deleteRow',
																			curNotEntityCategoryChildrenDatagrid
																					.datagrid(
																							'getRowIndex',
																							selectCurChildCategory));
														}
													} else {
														$.messager
																.alert(
																		'提示',
																		data.responseMessage,
																		'error');
													}
												}
											});
								}
							});

		}
	}

	function notEntityCategoryLayoutTogget(open) {
		if (open) {
			notEntityCategoryChildrenLayoutConfig.layout('expand', 'east');
		} else {
			notEntityCategoryChildrenLayoutConfig.layout('collapse', 'east');
		}

	}
</script>



<script type="text/javascript">
	$.extend($.fn.datagrid.methods, {
		editCell : function(jq, param) {
			return jq.each(function() {
				var opts = $(this).datagrid('options');
				var fields = $(this).datagrid('getColumnFields', true).concat(
						$(this).datagrid('getColumnFields'));
				for (var i = 0; i < fields.length; i++) {
					var col = $(this).datagrid('getColumnOption', fields[i]);
					col.editor1 = col.editor;
					if (fields[i] != param.field) {
						col.editor = null;
					}
				}
				$(this).datagrid('beginEdit', param.index);
				for (var i = 0; i < fields.length; i++) {
					var col = $(this).datagrid('getColumnOption', fields[i]);
					col.editor = col.editor1;
				}
			});
		}
	});
</script>